let resSearch = {}


import api from '../../utils/api';
// import { loadingTitle } from '../../utils/env';
Page({
    /**
     * 页面的初始数据
     */
    data: {
        value: '',
        resList: [],
        searchName: '',
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        resSearch = options;
        // type = options.type;
        console.log(options)
        this.setData({
            value: resSearch.value,
        });
        this.searchRemote();
    },
    select(e) {
        console.log(e)
        wx.setStorageSync('searchRes', e.target.dataset.res);
        wx.navigateBack();
    },
    inputChange(e) {
        this.setData({
            value: e.detail.value,
        });
        const searchObj = JSON.parse(JSON.stringify(resSearch))
        const keyList = Object.keys(searchObj).filter(key => key !== 'api' && key !== 'value')
        resSearch[keyList[0]] = e.detail.value
        this.searchRemote();
    },
    clear() {
        this.setData({
            value: '',
        });
        const searchObj = JSON.parse(JSON.stringify(resSearch))
        const keyList = Object.keys(searchObj).filter(key => key !== 'api' && key !== 'value')
        resSearch[keyList[0]] = ''
        this.searchRemote();
    },
    searchRemote() {
        wx.showLoading({
            title: '加载中',
        });
        console.log(resSearch)
        if (resSearch.api) {
            const searchObj = JSON.parse(JSON.stringify(resSearch))
            let resObj = {}
            Object.keys(searchObj).filter(key => key !== 'api' && key !== 'value').forEach(item => {
                resObj[item] = searchObj[item]
            })
            api[resSearch.api](resObj).then(res => {
                console.log(res)
                this.setData({
                    //如需通用需要修改接口格式
                    resList: res.data.map(item => {
                        return {
                            label: item,
                            value: item
                        }
                    }),
                })
            })

        }

        // if (this.data.resList.length === 1) {
        //     this.select({ target: { dataset: { res: this.data.resList[0] } } });
        // }
    },
});